#include "StdAfx.h"
#include "DeleteAgenda.h"
#include "UserDoesNotExist.h"
#include "InvalidPassword.h"
#include "UnableToDelete.h"
#include "MeetingDoesNotExist.h"

DeleteAgenda::DeleteAgenda(const string& name, const string& password, const string& title)
{
	this->name = name;
	this->password = password;
	this->title = title;
}


DeleteAgenda::~DeleteAgenda(void)
{
}

bool DeleteAgenda::doAction(UserCollection& uc, MeetingCollection& mc)
{
	/*guard */

	if( ! uc.hasUser(name) )
		throw UserDoesNotExist();
	if( ! uc.isValid(name, password) )
		throw InvalidPassword();

	if( !mc.hasMeeting(title) )
		throw MeetingDoesNotExist();

	Meeting meeting = mc.getMeetingByTitle(title);

	if( meeting.getInviter().getUserName() != name )
		throw UnableToDelete();

	/* delete there parts that has the meeting's information*/

	User &inviter = meeting.getInviter();
	User &invitee = meeting.getInvitee();
	
	int meetingId = mc.getIdByTitle(title);
	
	mc.deleteMeetingById(meetingId);		
	inviter.deleteMeetingId(meetingId);
	invitee.deleteMeetingId(meetingId);

	return true;
}//modify